package yxy.game.pm2.command.hero;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import yxy.apple.util.concurrent.ThreadPoolManager;
import yxy.cherry.battle.module.PropertyData;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.clout;
import yxy.cherry.data.bean.herodata;
import yxy.cherry.data.bean.herostagecost;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.stat.StatHandle;

import java.util.*;

@MessageType(proto = Command.HeroInfo, classtype = HeroMessage.HeroInfoRequest.class)
public class CMD_HeroInfo extends Command<HeroMessage.HeroInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, HeroMessage.HeroInfoRequest request) {
        Date date = new Date();

        try {
            String code = request.getCode();
            String playerCode = request.getPlayerCode();
            boolean self = false;
            if (playerCode == null || playerCode.length() == 0) {
                playerCode = messageSource.getTag();
                self = true;
            }

            HeroMessage.HeroInfoResponse.Builder response = HeroMessage.HeroInfoResponse.newBuilder();

            HeroHandle heroHandle = new HeroHandle();
            Hero hero = heroHandle.getHero(playerCode, code);
            if (hero == null) {
                send(messageSource, response, Result.HeroNotExists);// 武将不存在
                return;
            }

            HeroMessage.HeroInfo heroInfo = heroHandle.getHeroInfo(playerCode, hero);
            response.setInfo(heroInfo);

            //todo 是别人就改登录时间让别人3天清除缓存
            if (!self) {
                StatHandle.stat_用户登录状态更新(playerCode);
                send(messageSource, response, Result.Success);
            }

            if (self) {
                //新增升阶数值展示
                PropertyData propertyData = new BattleHandle().showPropertyDataToClassLevel(hero);
                HeroMessage.HeroProperty heroProperty = heroHandle.getHeroProperty(propertyData);
                response.setClassLevelProperty(heroProperty);

                //升阶的等级上限返回(最高阶时返回-1)
                int stageLevel = getStageLevel(hero.getStage(), hero.getStage() + 1);
                response.setStageLevel(stageLevel);

                //升阶条件是否满足提示
                heroHandle.showStageUpMsg(response, playerCode, code);

                //资质返回
                herodata data = DataCenter.getData(hero.getDataId(), herodata.class);
                response.setAptitude(data.getAptitude());

                send(messageSource, response, Result.Success);

                //todo 优化，红点改成异步推送
                String finalPlayerCode = playerCode;
                ThreadPoolManager.getInstance().execute(() -> {
                    heroHandle.checkTipTypeForHero(finalPlayerCode, hero);
                });
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        Date date1 = new Date();
        System.out.println("CMD_HeroInfo:" + (date1.getTime() - date.getTime()));
    }

    //升阶的等级上限返回
    private int getStageLevel(int stage, int netStage) {
        herostagecost nowStage = DataCenter.getData(stage, herostagecost.class);
        herostagecost nextStage = DataCenter.getData(netStage, herostagecost.class);
        if (nextStage == null) return -1;
        return nextStage.getCondition() - nowStage.getCondition();
    }

    //热门返回
    private HashMap<String, String> getHotList() {
        HashMap<String, String> returnMap = new HashMap<>();
        Map<String, clout> allData = DataCenter.getAllData(clout.class);
        JSONArray hotHeroArrary = new JSONArray();
        JSONArray hotHeroTeamArrary = new JSONArray();
        for (yxy.cherry.data.bean.clout clout : allData.values()) {
            if (clout.getType() == 1) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("type", clout.getType());
                jsonObject.put("heroId", clout.getHeroid()[0]);
                jsonObject.put("heroHeat", clout.getClout());
                jsonObject.put("detail", clout.getDetail());
                jsonObject.put("id", clout.getId());
                hotHeroArrary.add(jsonObject);

            }

            if (clout.getType() == 2) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("type", clout.getType());
                jsonObject.put("heroId", clout.getHeroid());
                jsonObject.put("heroHeat", clout.getClout());
                jsonObject.put("detail", clout.getDetail());
                jsonObject.put("id", clout.getId());
                hotHeroTeamArrary.add(jsonObject);
            }

        }


        Collections.sort(hotHeroArrary, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                JSONObject json1 = (JSONObject) o1;
                JSONObject json2 = (JSONObject) o2;
                Integer heroHeat1 = json1.getInteger("heroHeat");
                Integer heroHeat2 = json2.getInteger("heroHeat");
                return heroHeat2 - heroHeat1;
            }
        });


        Collections.sort(hotHeroTeamArrary, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                JSONObject json1 = (JSONObject) o1;
                JSONObject json2 = (JSONObject) o2;
                Integer heroHeat1 = json1.getInteger("heroHeat");
                Integer heroHeat2 = json2.getInteger("heroHeat");
                return heroHeat2 - heroHeat1;
            }
        });


        returnMap.put("hotHeroArrary", hotHeroArrary.toString());
        returnMap.put("hotHeroTeamArrary", hotHeroTeamArrary.toString());
        return returnMap;
    }


    private void send(MessageSource messageSource, HeroMessage.HeroInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
